package com.itheima.Demo;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Demo2G文件以下的读写 {
    public static void main(String[] args) throws IOException {
        //E:\
        long l = System.currentTimeMillis();

        RandomAccessFile f1 = new RandomAccessFile("E:\\1.zip", "r");
        RandomAccessFile f2 = new RandomAccessFile("C:\\Users\\97622\\Desktop\\复制.zip", "rw");
        //获取通道
        FileChannel c1 = f1.getChannel();
        FileChannel c2 = f2.getChannel();
        ////获取文件的大小
        long size = c1.size();
        //映射
        //第一个参数是：操作方式,第二个参数：从哪儿开始,第三个参数：个数
        MappedByteBuffer buffer1 = c1.map(FileChannel.MapMode.READ_ONLY, 0, size);
        MappedByteBuffer buffer2 = c2.map(FileChannel.MapMode.READ_WRITE, 0, size);
        //读写
        for (int i = 0; i < size; i++) {
            //从第一个数组中获取数据
            //放到第二个数组中
            buffer2.put(buffer1.get());
        }
        long l1 = System.currentTimeMillis();
        System.out.println(l1 - l);
        //关闭资源
        f1.close();
        f2.close();
        c1.close();
        c2.close();

    }
}
